//
//  EarbudsFindViewController.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/29.
//

import UIKit
import Lottie
import SwiftEntryKit
import CRPEarPhone

class EarbudsFindViewController: ViewController {
    
    /// 0左耳 1右耳
    private var playType: Int = -1

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigation.item.title = R.string.localizable.home_setting_looking_earbuds_title()
    }
    
    override func makeUI() {
        super.makeUI()
        
        self.view.addSubview(tipsView)
        tipsView.addSubview(titleLabel)
        self.view.addSubview(leftEarView)
        self.view.addSubview(lineView)
        self.view.addSubview(rightEarView)
        
        tipsView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(kNavHeight).priority(.high)
            make.leading.trailing.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.leading.equalToSuperview()
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-15)
        }

        lineView.snp.makeConstraints { make in
            make.top.equalTo(tipsView.snp.bottom)
            make.centerX.equalToSuperview()
            make.width.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
        leftEarView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.trailing.equalTo(lineView.snp.leading)
            make.top.equalTo(lineView)
        }
        
        rightEarView.snp.makeConstraints { make in
            make.leading.equalTo(lineView.snp.trailing)
            make.trailing.equalToSuperview()
            make.top.equalTo(lineView)
        }
        
        showPopupView()
    }
    
    @objc private func leftEarPlayClick(sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            self.leftEarView.animationImageView.play()
            BluetoothManager.shared.sendFindEarPhone(state: 1, leftEar: 1, rightEar: 0)
        } else {
            self.leftEarView.animationImageView.stop()
            BluetoothManager.shared.sendFindEarPhone(state: 0, leftEar: 0, rightEar: 0)
        }
        self.rightEarView.playButton.isEnabled = !sender.isSelected
    }
    
    @objc private func rightEarPlayClick(sender: UIButton) {
        
        sender.isSelected = !sender.isSelected
        if sender.isSelected {
            self.rightEarView.animationImageView.play()
            BluetoothManager.shared.sendFindEarPhone(state: 1, leftEar: 0, rightEar: 1)
        } else {
            self.rightEarView.animationImageView.stop()
            BluetoothManager.shared.sendFindEarPhone(state: 0, leftEar: 0, rightEar: 0)
        }
        self.leftEarView.playButton.isEnabled = !sender.isSelected
    }
    
    private func showPopupView() {
        var attributes = EKAttributes.bottomToast
        attributes.position = .bottom
        attributes.entryBackground = .color(color: .white)
        attributes.screenBackground = .color(color: EKColor(UIColor(white: 0, alpha: 0.5)))
        //遮罩层触摸是忽略、转发到窗口还是退出
        attributes.screenInteraction = .absorbTouches
        attributes.entryInteraction = .forward
        attributes.entranceAnimation = .translation
        attributes.exitAnimation = .translation
        attributes.displayDuration = .infinity
        
        SwiftEntryKit.display(entry:self.popView, using: attributes)

        let view = UIView()
        view.addSubview(self.popView)
        self.popView.confirmBlock = {
            SwiftEntryKit.dismiss()
        }
        self.popView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.leading.equalToSuperview()
            make.bottom.equalToSuperview()
        }
    }
    
    private lazy var popView: EarbudsFindPopView = {
        let view = EarbudsFindPopView()
        return view
    }()
    
    private lazy var tipsView: View = {
        let view = View()
        view.backgroundColor = R.color.common_7_bg()
        return view
    }()

    private lazy var titleLabel: Label = {
        let label = Label()
        label.font = Fonts.systemFont(ofSize: 14)
        label.textColor = R.color.common_8_text()
        label.text = R.string.localizable.earbuds_find_tips()
        label.textAlignment = .center
        return label
    }()
    
    private lazy var leftEarView: EarbudsFindView = {
        let view = EarbudsFindView()
        view.earButton.setTitle(R.string.localizable.earbuds_find_left_ear_text(), for: .normal)
        view.earbudsImageView.image = R.image.img_earbuds_find_left()
        view.playButton.addTarget(self, action: #selector(leftEarPlayClick(sender:)), for: .touchUpInside)
        return view
    }()
    
    private lazy var lineView: View = {
        let view = View()
        view.backgroundColor = R.color.common_6_line()
        return view
    }()
    
    private lazy var rightEarView: EarbudsFindView = {
        let view = EarbudsFindView()
        view.earButton.setTitle(R.string.localizable.earbuds_find_right_ear_text(), for: .normal)
        view.earbudsImageView.image = R.image.img_earbuds_find_right()
        view.playButton.addTarget(self, action: #selector(rightEarPlayClick(sender:)), for: .touchUpInside)
        return view
    }()
}

class EarbudsFindView: View {
    
    override func makeUI() {
        super.makeUI()
        
        self.addSubview(animationImageView)
        self.addSubview(earbudsImageView)
        self.addSubview(earButton)
        self.addSubview(playButton)

        animationImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(112)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(88)
        }
        
        earbudsImageView.snp.makeConstraints { make in
            make.center.equalTo(animationImageView)
        }
        
        earButton.snp.makeConstraints { make in
            make.top.equalTo(animationImageView.snp.bottom).offset(76)
            make.height.equalTo(28)
            make.centerX.equalToSuperview()
        }
        
        playButton.snp.makeConstraints { make in
            make.top.equalTo(earButton).offset(105)
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
    }
    
    public lazy var animationImageView: LottieAnimationView = {
        let animation = LottieAnimation.named("ani_earbuds_find_ing")
        let lot = LottieAnimationView(animation: animation)
        lot.contentMode = .scaleAspectFit
        lot.loopMode = .loop
        return lot
    }()
    
    public lazy var earButton: UIButton = {
        let button = UIButton()
        button.setTitleColor(R.color.common_3_text(), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        button.titleLabel?.numberOfLines = 0
        button.contentEdgeInsets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        button.backgroundColor = R.color.common_12_assist()
        button.layer.cornerRadius = 14
        return button
    }()
    
    public lazy var earbudsImageView: ImageView = {
        let imageView = ImageView()
        return imageView
    }()
    
    public lazy var playButton: UIButton = {
        let button = UIButton()
        button.setImage(R.image.ic_earbuds_find_pause(), for: .normal)
        button.setImage(R.image.ic_earbuds_find_play(), for: .selected)
        return button
    }()
}
